<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.transName" clearable placeholder="模板名称" />
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="success" icon="el-icon-search" @click="getDataList()">查询</el-button>
        <el-button
          v-has-permission="['shop:transport:save']"
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="addOrUpdateHandle()"
        >新增</el-button>
        <el-button
          v-has-permission="['shop:transport:delete']"
          :disabled="dataListSelections.length <= 0"
          size="small"
          type="danger"
          @click="deleteHandle()"
        >批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column
        align="center"
        header-align="center"
        type="selection"
        width="50"
      />
      <el-table-column
        align="center"
        header-align="center"
        label="模板名称"
        prop="transName"
      />
      <el-table-column
        align="center"
        fixed="right"
        header-align="center"
        label="操作"
        width="150"
      >
        <template slot-scope="scope">
          <el-dropdown size="mini" split-button type="primary" trigger="click">
            操作
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="small" type="primary" icon="el-icon-edit" style="width: 100%; margin: 5px 0" plain @click="addOrUpdateHandle(scope.row.transportId)">修改</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="small" type="danger" icon="el-icon-delete" style="width: 100%; margin: 5px 0" plain @click="deleteHandle(scope.row.transportId)">删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    />
  </div>
</template>

<script>
import AddOrUpdate from './transport-add-or-update'
import { delTransport, listPageTransport } from '@/api/oms/transport'

export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      dataForm: {
        transName: ''
      },
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList(page, params) {
      this.dataListLoading = true
      const data = Object.assign(
        {
          pageNum: this.pageIndex,
          pageSize: this.pageSize
        },
        params
      )
      listPageTransport(data).then(res => {
        this.dataList = res.data.list
        this.totalPage = res.data.totalCount
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      const ids = id ? [id] : this.dataListSelections.map(item => {
        return item.transportId
      })
      this.$confirm(
        `确定进行[${id ? '删除' : '批量删除'}]操作?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delTransport(ids).then(res => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              // this.getDataList(this.page)
              this.refreshChange()
            }
          })
        })
      }).catch((e) => {
        console.log('e: ', e)
      })
    },

    // 条件查询
    searchChange(params) {
      this.getDataList(this.page, params)
    },
    // 刷新回调用
    refreshChange() {
      this.getDataList(this.pageIndex)
      this.dataListSelections = []
    },
    // 多选变化
    selectionChange(val) {
      console.log('val: ', val)
      this.dataListSelections = val
    }
  }
}
</script>
